package com.weimei.conversionTreasure.viewModel;

import android.app.Application;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableDouble;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.activity.AddBudgetActivity;
import com.weimei.conversionTreasure.activity.AddMarketActivity;
import com.weimei.conversionTreasure.activity.LoginActivity;
import com.weimei.conversionTreasure.activity.TotalActivity;
import com.weimei.conversionTreasure.http.Repository;
import com.weimei.conversionTreasure.http.entity.BillBean;
import com.weimei.conversionTreasure.http.entity.PaoLiangListBean;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.utils.ValidateUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class TabTwoVM extends BaseViewModel<Repository> {

    public BindingCommand add = new BindingCommand(() -> {
        if (UserUtils.isLogin()){
            startActivity(AddMarketActivity.class);
        }else {
            startActivity(LoginActivity.class);
        }
    });

    public BindingCommand addYs = new BindingCommand(() -> {
        if (UserUtils.isLogin()) {
            startActivity(AddBudgetActivity.class);
        }else {
            startActivity(LoginActivity.class);
        }
    });

    public BindingCommand total = new BindingCommand(() -> {
        if (UserUtils.isLogin()) {
            startActivity(TotalActivity.class);
        }else {
            startActivity(LoginActivity.class);
        }
    });

    public ObservableField<String> zcNum = new ObservableField<>("");
    public ObservableField<String> srNum = new ObservableField<>("");
    public ObservableField<String> jyNum = new ObservableField<>("");

    public ObservableField<String> ysNum = new ObservableField<>("");
    public ObservableField<String> syNum = new ObservableField<>("");

    public ObservableInt pro = new ObservableInt();
    public ObservableInt proMax = new ObservableInt();

    public TabTwoVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();

    }

    public ObservableList<BillItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<BillItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_bill);

    public void getData(){
        observableList.clear();
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        model.mybill(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BillBean>() {
                    @Override
                    public void onNext(BillBean bean) {
                        if (bean.issucc){
                            zcNum.set(bean.getData().getExpend()+"");
                            srNum.set("本月收入："+bean.getData().getIncome()+"");
                            jyNum.set("本月结余："+bean.getData().getBalance());

                            syNum.set("剩余："+bean.getData().getBalance());
                            ysNum.set("总金额："+bean.getData().getBudget());

                            if (ValidateUtils.isNotEmptyString(bean.getData().getBudget()+"") &&
                                    ValidateUtils.isNotEmptyString(bean.getData().getBalance()+"") ) {
                                proMax.set(bean.getData().getBudget());
                                pro.set(bean.getData().getBalance());
                            }

                            if (bean.getData().getDlist().size() > 0){
                                for (BillBean.DataDTO.DlistDTO dlistDTO: bean.getData().getDlist()) {
                                    BillItemVM itemVM = new BillItemVM(TabTwoVM.this,dlistDTO);
                                    observableList.add(itemVM);
                                }
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
